package com.java.service.impl;

import com.java.entity.Question;
import com.java.entity.QuestionExample;
import com.java.mapper.QuestionMapper;
import com.java.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public int insert(Question question) {
        return questionMapper.insertSelective(question);
    }

    @Override
    public int delete(Integer questionId) {
        return questionMapper.deleteByPrimaryKey(questionId);
    }

    @Override
    public int update(Question question) {
        return questionMapper.updateByPrimaryKeySelective(question);
    }

    @Override
    public Question selectOne(Integer questionId) {
        Question question = questionMapper.selectByPrimaryKey(questionId);
        if (question == null) {
            throw new RuntimeException("question is null, questionId=" + questionId);
        }
        return question;
    }

    @Override
    public List<Question> selectAll() {
        return questionMapper.selectByExample(null);
    }

    @Override
    public List<Question> selectByQuestionTypeId(Integer questionTypeId) {
        QuestionExample example = new QuestionExample();
        example.or().andQuestionTypeIdEqualTo(questionTypeId);
        return questionMapper.selectByExample(example);
    }

}
